package com.atguigu.blob;

import com.atguigu.bean.Customer;
import com.atguigu.util.JDBCUtils;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

/**
 * 测试使用 PreParedStatement 操作 Blob 类型的数据
 *
 * @author admin
 */
public class BlobTest {

    /**
     * 向数据表 Customers 中插入 Blob 类型的字段
     */
    @Test
    public void testInsert() throws Exception {
        // 1.获取连接
        Connection connection = JDBCUtils.getConnection();

        // 2.预编译 sql 语句，返回 PreparedStatement 实例
        String sql = "insert into customers(name, email, birth, photo) value(?, ?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        // 3.填充占位符
        preparedStatement.setObject(1, "雄霸");
        preparedStatement.setObject(2, "dishitian@qq.com");
        preparedStatement.setObject(3, "1990-02-02");
        FileInputStream inputStream = new FileInputStream("girl.jpg");
        preparedStatement.setBlob(4, inputStream);

        // 4.执行
        preparedStatement.execute();

        // 5.资源的关闭
        JDBCUtils.closeResource(connection, preparedStatement);

    }

    /**
     * 查询数据表 customers 中 Blob 类型的字段
     */
    @Test
    public void testQuery() throws Exception {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        InputStream inputStream = null;
        FileOutputStream outputStream = null;

        try {
            // 1.获取连接
            connection = JDBCUtils.getConnection();

            // 2.预编译 sql 语句，返回 PreparedStatement 实例
            String sql = "select id, name, email, birth, photo from customers where id = ?";
            preparedStatement = connection.prepareStatement(sql);

            // 3.填充占位符
            preparedStatement.setInt(1, 21);

            // 4.执行，并返回结果集
            resultSet = preparedStatement.executeQuery();

            // 5.处理结果集
            if (resultSet.next()) {

                // 方式一：
                // int id = resultSet.getInt(1);
                // String name = resultSet.getString(2);
                // String email = resultSet.getString(3);
                // Date birth = resultSet.getDate(4);

                // 方式二：
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                Date birth = resultSet.getDate("birth");

                Customer customer = new Customer(id, name, email, birth);
                System.out.println(customer);

                // 将 Blob 类型的字段下载下来，以文件的方式保存到本地
                Blob photo = resultSet.getBlob("photo");
                inputStream = photo.getBinaryStream();
                outputStream = new FileOutputStream("dishitian.jpeg");
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 6.关闭资源
            JDBCUtils.closeResource(connection, preparedStatement, resultSet);
        }
    }
}
